package com.foodchain.controller;

import com.foodchain.entity.*;
import com.foodchain.service.*;
import com.foodchain.util.IsPhone;
import com.foodchain.vo.Register;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yuanZ;
 * @Date: 2018/9/10 15:51;
 * @Description: 用户注册;
**/
@Controller
@RequestMapping("/register")
public class RegisterController extends BaseController {

    @Resource
    private UserService userService;

   /*
    * 功能描述:用户注册
    * @param:  * @param request
    * @param register
    * @return: java.lang.String
    * @auther: duyuanyuan
    * @date: 2019/3/27 12:56
    */
    @ResponseBody
    @RequestMapping("/saveUser")
    public String saveUser(HttpServletRequest request,Register register) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        // 检查该手机号是否已经注册
        User isHaveUser = userService.findByUserName(register.getUserName());
        if (null != isHaveUser) {
            return JsonCode(1, "该手机号已经注册，请直接登陆", null);
        }
        boolean b = IsPhone.JudgeIsMoblie(request);
        if(b){
            // 手机端保存用户
            User user = userService.saveRegisterUser(register);
            Map map = new HashMap();
            map.put("userId", user.getId());
            return JsonCode(0, "success", map);
        }
        // PC端保存用户
        User user = userService.saveRegister(register);
        Map map = new HashMap();
        map.put("userId", user.getId());
        return JsonCode(0, "success", map);
    }

}
